//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DigitalIborCoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/IborCoupon.h>
#include <gen/QL/Cashflows/DigitalReplication.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/FloatingRateCouponPricer.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/InterestRateIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/DigitalCoupon.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CDigitalIborCoupon::CDigitalIborCoupon (Cephei::QL::Cashflows::IIborCoupon^ underlying, Microsoft::FSharp::Core::FSharpOption<Double>^ callStrike, Microsoft::FSharp::Core::FSharpOption<QL::Position::TypeEnum>^ callPosition, Microsoft::FSharp::Core::FSharpOption<Boolean>^ isCallATMIncluded, Microsoft::FSharp::Core::FSharpOption<Double>^ callDigitalPayoff, Microsoft::FSharp::Core::FSharpOption<Double>^ putStrike, Microsoft::FSharp::Core::FSharpOption<QL::Position::TypeEnum>^ putPosition, Microsoft::FSharp::Core::FSharpOption<Boolean>^ isPutATMIncluded, Microsoft::FSharp::Core::FSharpOption<Double>^ putDigitalPayoff, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Cashflows::IDigitalReplication^>^ replication, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer) : CDigitalCoupon(CDigitalIborCoupon::typeid)
{
    CIborCoupon^ _Cunderlying;
    CDigitalReplication^ _Creplication;
    try
    {
#ifdef HANDLE
        _phDigitalIborCoupon = NULL;
#endif
        _Cunderlying = safe_cast<CIborCoupon^> (underlying);
        _Cunderlying->Lock();
        boost::shared_ptr<QuantLib::IborCoupon>& _underlying = static_cast<boost::shared_ptr<QuantLib::IborCoupon>&> (_Cunderlying->GetShared ()); 
        QuantLib::Rate _callStrike = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (callStrike) ? (QuantLib::Rate)ValueHelper::Convert (callStrike->Value) : Null<QuantLib::Rate>()); //4
        QuantLib::Position::Type _callPosition = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Position::TypeEnum>::IsSome::get (callPosition) ? (QuantLib::Position::Type)callPosition->Value : QuantLib::Position::Type::Long); //10
        bool _isCallATMIncluded = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (isCallATMIncluded) ? (bool)ValueHelper::Convert (isCallATMIncluded->Value) : false); //4
        QuantLib::Rate _callDigitalPayoff = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (callDigitalPayoff) ? (QuantLib::Rate)ValueHelper::Convert (callDigitalPayoff->Value) : Null<QuantLib::Rate>()); //4
        QuantLib::Rate _putStrike = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (putStrike) ? (QuantLib::Rate)ValueHelper::Convert (putStrike->Value) : Null<QuantLib::Rate>()); //4
        QuantLib::Position::Type _putPosition = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Position::TypeEnum>::IsSome::get (putPosition) ? (QuantLib::Position::Type)putPosition->Value : QuantLib::Position::Type::Long); //10
        bool _isPutATMIncluded = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (isPutATMIncluded) ? (bool)ValueHelper::Convert (isPutATMIncluded->Value) : false); //4
        QuantLib::Rate _putDigitalPayoff = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (putDigitalPayoff) ? (QuantLib::Rate)ValueHelper::Convert (putDigitalPayoff->Value) : Null<QuantLib::Rate>()); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Cashflows::IDigitalReplication^>::IsSome::get (replication))
        {
            _Creplication = safe_cast<CDigitalReplication^> (replication->Value);
            _Creplication->Lock();
        }
        boost::shared_ptr<QuantLib::DigitalReplication>& _replication = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Cashflows::IDigitalReplication^>::IsSome::get (replication) ? static_cast<boost::shared_ptr<QuantLib::DigitalReplication>&> (_Creplication->GetShared ()) : boost::shared_ptr<QuantLib::DigitalReplication>()); //1
        _ppDigitalIborCoupon = new boost::shared_ptr<QuantLib::DigitalIborCoupon> (new QuantLib::DigitalIborCoupon ( _underlying,  _callStrike,  _callPosition,  _isCallATMIncluded,  _callDigitalPayoff,  _putStrike,  _putPosition,  _isPutATMIncluded,  _putDigitalPayoff,  _replication ));
        CFloatingRateCouponPricer^ _CQL_Pricer = safe_cast<CFloatingRateCouponPricer^> (QL_Pricer);
        boost::shared_ptr<QuantLib::FloatingRateCouponPricer>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::FloatingRateCouponPricer>&> (_CQL_Pricer->GetShared ());
        (*_ppDigitalIborCoupon)->setPricer (_QL_Pricer);
        SetDigitalCoupon (boost::dynamic_pointer_cast<QuantLib::DigitalCoupon> (*_ppDigitalIborCoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cunderlying != nullptr) _Cunderlying->Unlock();
        if (_Creplication != nullptr) _Creplication->Unlock();
    }
}
Cephei::QL::Cashflows::CDigitalIborCoupon::CDigitalIborCoupon (boost::shared_ptr<QuantLib::DigitalIborCoupon>& childNative, Object^ owner) : CDigitalCoupon(CDigitalIborCoupon::typeid)
{
#ifdef HANDLE
	_phDigitalIborCoupon = NULL;
#endif
	_ppDigitalIborCoupon = &childNative;
    _ppDigitalCoupon = new boost::shared_ptr<QuantLib::DigitalCoupon> (boost::dynamic_pointer_cast<QuantLib::DigitalCoupon> (*_ppDigitalIborCoupon));
}
Cephei::QL::Cashflows::CDigitalIborCoupon::CDigitalIborCoupon (QuantLib::DigitalIborCoupon& childNative, Object^ owner) : CDigitalCoupon(CDigitalIborCoupon::typeid)
{
#ifdef HANDLE
	_phDigitalIborCoupon = NULL;
#endif
	_ppDigitalIborCoupon = new boost::shared_ptr<QuantLib::DigitalIborCoupon> (&childNative);
    _ppDigitalCoupon = new boost::shared_ptr<QuantLib::DigitalCoupon> (boost::dynamic_pointer_cast<QuantLib::DigitalCoupon> (*_ppDigitalIborCoupon));
    _DigitalIborCouponOwner = owner;
    _DigitalCouponOwner = owner;
}

Cephei::QL::Cashflows::CDigitalIborCoupon::CDigitalIborCoupon (CDigitalIborCoupon^ copy) : CDigitalCoupon(CDigitalIborCoupon::typeid)
{
#ifdef HANDLE
	_phDigitalIborCoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDigitalIborCoupon = new boost::shared_ptr<QuantLib::DigitalIborCoupon> (copy->GetShared());
        _ppDigitalCoupon = new boost::shared_ptr<QuantLib::DigitalCoupon> (boost::dynamic_pointer_cast<QuantLib::DigitalCoupon> (*_ppDigitalIborCoupon));
    }
}
Cephei::QL::Cashflows::CDigitalIborCoupon::CDigitalIborCoupon (System::Type^ t) : CDigitalCoupon(CDigitalIborCoupon::typeid)
{
#ifdef HANDLE
	_phDigitalIborCoupon = NULL;
#endif
	if (!t->IsSubclassOf(CDigitalIborCoupon::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CDigitalIborCoupon::CDigitalIborCoupon (QuantLib::Handle<QuantLib::DigitalIborCoupon>& childNative, Object^ owner)  : CDigitalCoupon(CDigitalIborCoupon::typeid)
{
	_phDigitalIborCoupon = &childNative;
	_ppDigitalIborCoupon = &static_cast<boost::shared_ptr<QuantLib::DigitalIborCoupon>>(childNative.currentLink());
    _ppDigitalCoupon = new boost::shared_ptr<QuantLib::DigitalCoupon> (boost::dynamic_pointer_cast<QuantLib::DigitalCoupon> (*_ppDigitalIborCoupon));
    _DigitalIborCouponOwner = owner;
}
Cephei::QL::Cashflows::CDigitalIborCoupon::CDigitalIborCoupon (QuantLib::Handle<QuantLib::DigitalIborCoupon> childNative)  : CDigitalCoupon(CDigitalIborCoupon::typeid)
{
	_phDigitalIborCoupon = &childNative;
	_ppDigitalIborCoupon = &static_cast<boost::shared_ptr<QuantLib::DigitalIborCoupon>>(childNative.currentLink());
    _ppDigitalCoupon = new boost::shared_ptr<QuantLib::DigitalCoupon> (boost::dynamic_pointer_cast<QuantLib::DigitalCoupon> (*_ppDigitalIborCoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CDigitalIborCoupon::CDigitalIborCoupon (QuantLib::DigitalIborCoupon childNative)  : CDigitalCoupon(CDigitalIborCoupon::typeid)
{
#ifdef HANDLE
	_phDigitalIborCoupon = NULL;
#endif
	_ppDigitalIborCoupon = new boost::shared_ptr<QuantLib::DigitalIborCoupon> (new QuantLib::DigitalIborCoupon (childNative));
    _ppDigitalCoupon = new boost::shared_ptr<QuantLib::DigitalCoupon> (boost::dynamic_pointer_cast<QuantLib::DigitalCoupon> (*_ppDigitalIborCoupon));
}
#endif

Cephei::QL::Cashflows::CDigitalIborCoupon::~CDigitalIborCoupon ()
{
    if (_ppDigitalIborCoupon != NULL)
    {
	    delete _ppDigitalIborCoupon;
        _ppDigitalIborCoupon = NULL;
    }
}
Cephei::QL::Cashflows::CDigitalIborCoupon::!CDigitalIborCoupon ()
{
    if (_ppDigitalIborCoupon != NULL)
    {
	    delete _ppDigitalIborCoupon;
    }
}
QuantLib::DigitalIborCoupon& Cephei::QL::Cashflows::CDigitalIborCoupon::GetReference ()
{
    if (_ppDigitalIborCoupon == NULL) throw gcnew NativeNullException ();
	return **_ppDigitalIborCoupon;
}
boost::shared_ptr<QuantLib::DigitalIborCoupon>& Cephei::QL::Cashflows::CDigitalIborCoupon::GetShared ()
{
    if (_ppDigitalIborCoupon == NULL) throw gcnew NativeNullException ();
	return *_ppDigitalIborCoupon;
}
QuantLib::DigitalIborCoupon* Cephei::QL::Cashflows::CDigitalIborCoupon::GetPointer ()
{
    if (_ppDigitalIborCoupon == NULL) throw gcnew NativeNullException ();
	return &**_ppDigitalIborCoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DigitalIborCoupon>& Cephei::QL::Cashflows::CDigitalIborCoupon::GetHandle ()
{
	if (_phDigitalIborCoupon == NULL)
	{
		_phDigitalIborCoupon = new Handle<QuantLib::DigitalIborCoupon> (*_ppDigitalIborCoupon);
	}
	return *_phDigitalIborCoupon;
}
#endif
bool Cephei::QL::Cashflows::CDigitalIborCoupon::HasNative () 
{
	return (_ppDigitalIborCoupon != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IDigitalIborCoupon^ Cephei::QL::Cashflows::CDigitalIborCoupon_Factory::Create (Cephei::QL::Cashflows::IIborCoupon^ underlying, Microsoft::FSharp::Core::FSharpOption<Double>^ callStrike, Microsoft::FSharp::Core::FSharpOption<QL::Position::TypeEnum>^ callPosition, Microsoft::FSharp::Core::FSharpOption<Boolean>^ isCallATMIncluded, Microsoft::FSharp::Core::FSharpOption<Double>^ callDigitalPayoff, Microsoft::FSharp::Core::FSharpOption<Double>^ putStrike, Microsoft::FSharp::Core::FSharpOption<QL::Position::TypeEnum>^ putPosition, Microsoft::FSharp::Core::FSharpOption<Boolean>^ isPutATMIncluded, Microsoft::FSharp::Core::FSharpOption<Double>^ putDigitalPayoff, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Cashflows::IDigitalReplication^>^ replication, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer)
{
    return gcnew CDigitalIborCoupon ( underlying,  callStrike,  callPosition,  isCallATMIncluded,  callDigitalPayoff,  putStrike,  putPosition,  isPutATMIncluded,  putDigitalPayoff,  replication,  QL_Pricer);
}
